<?php
/**
 * @desc 运费模板管理
 * @author kevin wang
 * @date 2014-10-25
 */
class ctrl_transporttmp extends ctrl_base {
    private static $dirName = 'transporttmp';
    
    /**
     * @desc 地区列表
	 * @author kevin wang
	 * @param void
	 * @return void
     */
    public static function main() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('stmp_list'));
		//页面输入参数处理
    	$currentPage = isset($_POST['pageNum']) ? $_POST['pageNum'] : 1;
    	$pagesize = isset($_POST['numPerPage']) ? $_POST['numPerPage'] : self::$_pagesize;
		$nextpage = ($currentPage - 1) * $pagesize;

    	$tmp_name = isset($_POST['tmp_name']) ? trim($_POST['tmp_name']) : '';
		$tmp_type = isset($_POST['tmp_type']) ? intval($_POST['tmp_type']) : 0;
    	//搜索条件组合
		$condition = array();
    	if(!empty($tmp_name)) {
    		$condition['tmp_name'] = $tmp_name;
    	}
		if(!empty($tmp_type)) {
    		$condition['tmp_type'] = $tmp_type;
    	}
		//订单获取
    	$area_list = model_transporttmp :: get_transporttmp_list($nextpage,$pagesize,$condition);
    	$total = array_pop($area_list);
		//模板赋值
    	self::smarty() -> assign('area_list', $area_list);
		self::smarty() -> assign('total', $total);
		self::smarty() -> assign('pageNum', $currentPage);
		self::smarty() -> assign('numPerPage', $pagesize);
		self::smarty() -> assign('pageNumShown', self::$_pageNumShown);
		self::smarty() -> assign('tmp_name', $tmp_name);
		self::smarty() -> assign('tmp_type', $tmp_type);
		self::smarty() -> assign('shoNum', self::$_shoNum);
		
    	self::smarty() -> display(self::$dirName . '/transporttmp_list.html');    	
    }
	//添加地区
	public static function transporttmp_add(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		self::operation(array('stmp_add'));
		if($_POST){

			$transporttmp = self::get_request_data();
			$transporttmp['section'] = self::handle_section($transporttmp['min'], $transporttmp['max']);
			unset($transporttmp['min'], $transporttmp['max'], $transporttmp['dwz_rowNum']);
			$transporttmp['author'] = $_SESSION['admin']['username'];
			$transporttmp['editer'] = $_SESSION['admin']['username'];

			if(model_transporttmp::transporttmp_exists($transporttmp['tmp_name'], $transporttmp['tmp_type'])){
				self::ajax_ouput(300, '添加失败,该运费模板已存在');
			}
			$result = model_transporttmp::transporttmp_add($transporttmp);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','transporttmp','closeCurrent');
			}else{
				self::ajax_ouput(300, '添加失败');
			}
		}
		self::smarty() -> display(self::$dirName . '/transporttmp_add.html');
	}
	//删除运费模板
	public static function transporttmp_delete() {
    	if(!model_admin::islogin()) {
			self::goto_login();
    	}
		self::operation(array('stmp_delete'));
    	$ids = $_POST['ids'];
		$ids = explode(",", $ids);
		foreach($ids as $v){
			$v = intval($v);
			$transporttmp = model_transporttmp::get_transporttmp_by_id($v);
			$tmp = model_produce::get_produce_by_tmpid($v);
			if(!empty($tmp)){
				self::ajax_ouput(300, "操作失败,产品小类有 {$transporttmp['tmp_name']} 的模板关联");
			}
			$id[] = $v;
		}
    	$data = array('delete_flag' => 1);

    	$st = model_transporttmp::update_transporttmp_info($id, $data);

    	if($st > 0) {
			self::ajax_ouput(200, '操作成功','transporttmp');
    	} else {
			self::ajax_ouput(300, '操作失败');
    	}
    }
	//编辑区域
	 public static function transporttmp_edit() {
    	if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$id = isset($_GET['id'])? (intval($_GET['id'])):(intval($_POST['id']));
    	if($id <= 0) {
			self::ajax_ouput(300, '非法访问');
    	}
		$transporttmp = model_transporttmp::get_transporttmp_by_id($id);
		$ctrl_tag = 'stmp_edit_other';
		if($transporttmp['author'] == $_SESSION['admin']['username']){
			$ctrl_tag = 'stmp_edit_self';
		}
		self::operation(array($ctrl_tag));
    	if($_POST){
			$tmp_type = $transporttmp['tmp_type'];
			$transporttmp = array();

			$transporttmp = self::get_request_data();
			if($tmp_type != $transporttmp['tmp_type'] && model_transportprice::get_transportprice_by_tmp_id($id)){
				self::ajax_ouput(300, '修改失败,该模板已设置运费,不能修改模板类型');
			}
			$transporttmp['section'] = self::handle_section($transporttmp['min'], $transporttmp['max']);
			unset($transporttmp['min'], $transporttmp['max'], $transporttmp['dwz_rowNum']);
			$transporttmp['editer'] = $_SESSION['admin']['username'];
			unset($transporttmp['id']);
			if(model_transporttmp::transporttmp_exists($transporttmp['tmp_name'], $transporttmp['tmp_type'], $id)){
				self::ajax_ouput(300, '修改失败,该模板已存在');
			}
			$result = model_transporttmp::update_transporttmp_info(explode(',',$id), $transporttmp);
	    	
		    if(!empty($result)){
				$areas = model_transportprice::get_select_list($id);
				if(!empty($areas)){
					$areas = array_unique($areas);
					model_transportprice::modify_tmp_addr($id, $areas, json_decode($transporttmp['section'], true));
				}
				self::ajax_ouput(200, '操作成功','transporttmp','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}    		
    	}
		$transporttmp['section'] = json_decode($transporttmp['section'], true);
    	self::smarty() -> assign('transporttmp', $transporttmp);
    	self::smarty() -> display(self::$dirName . '/transporttmp_edit.html');
    }
	//处理吨位区间
	private static function handle_section(array $min, array $max){
		$return = array();
		if(empty($min) || empty($max)){
			self::ajax_ouput(300, '操作失败,吨位区间不能为空');
		}
		$current = 0;
		$i = 0;
		foreach($min as $key=>$value){
			$maxvalue = $max[$key];
			if($value != $current || empty($maxvalue) || $value >= $maxvalue){
				self::ajax_ouput(300, '操作失败,吨位区间有误');
			}
			$current = $maxvalue;
			$return[$i]['min'] = $value;
			$return[$i]['max'] = $maxvalue;
			$i++;
		}
		return json_encode($return);
	}
	//模板到货地设置
	public static function setarea(){
		if(!model_admin::islogin()) {
    		self::goto_login();
    	}
		$tmp_id = intval($_REQUEST['tmp_id']);
		$transporttmp = model_transporttmp::get_transporttmp_by_id($tmp_id);
		if($_POST){
			$area = self::get_request_data();
			$section = json_decode($transporttmp['section'], true);
			if(empty($tmp_id)){
				self::ajax_ouput(300, '操作失败,模板ID不能为空');
			}
			if(empty($area['area'])){
				self::ajax_ouput(300, '操作失败,地区不能空');
			}
			if(empty($section)){
				self::ajax_ouput(300, '操作失败,尚未设置吨位区间');
			}
			$result = model_transportprice::modify_tmp_addr($tmp_id, $area['area'], $section);
			if(!empty($result)){
				self::ajax_ouput(200, '操作成功','transporttmp','closeCurrent');
			}else{
				self::ajax_ouput(300, '操作失败');
			}
		}
		switch($transporttmp['tmp_type']){
			case 1://汽运地址
				$list = model_motor::get_all_motor();
				break;
			case 2://火运地址
				$list = model_train::get_all_train();
				break;
			case 3://水运地址
				$list = model_ship::get_all_ship();
				break;
			case 4://水汽联运地址
				$list = model_smotor::get_all_smotor();
				break;
			default://默认汽运地址
				$list = model_motor::get_all_motor();
				break;
		}
		$list = self::area_by_group($list);
		$user_select = model_transportprice::get_select_list($tmp_id);
		$user_select = array_unique($user_select);
		self::smarty() -> assign("select",$user_select);
		self::smarty() -> assign('list', $list);
		self::smarty() -> assign('tmp_id', $tmp_id);
		self::smarty() -> display(self::$dirName . '/setarea.html');
	}
	//地址分组
	private static function area_by_group(array $area){
		$return = array();
		$provice = array();
		$list = array();
		foreach($area as $key => $value){
			$provice[] = $value['province'];
			if(!empty($value['area'])){
				$str = $value['area'];
				if(!empty($value['street'])){
					$str = $value['area'].'/'.$value['street'];
				}
				$list[$value['province']][$value['city']][$str]['id'] = $value['area_motor_id'];
				$list[$value['province']][$value['city']][$str]['name'] = "{$value['province']}/{$value['city']}/{$str}";
			}elseif(!empty($value['area_station'])){
				$str = $value['area_station'];
				$list[$value['province']][$value['city']][$str]['id'] = $value['area_train_id'];
				$list[$value['province']][$value['city']][$str]['name'] = "{$value['province']}/{$value['city']}/{$str}";
			}elseif(!empty($value['port'])){
				$str = $value['port'];
				if(!empty($value['port_dest'])){
					$str = $value['port'].'/'.$value['port_dest'];
				}
				$list[$value['province']][$value['city']][$str]['id'] = $value['area_ship_id'];
				$list[$value['province']][$value['city']][$str]['name'] = "{$value['province']}/{$value['city']}/{$str}";
			}elseif(!empty($value['area_wharf'])){
				$str = $value['area_wharf'];
				$list[$value['province']][$value['city']][$str]['id'] = $value['area_smotor_id'];
				$list[$value['province']][$value['city']][$str]['name'] = "{$value['province']}/{$value['city']}/{$str}";
			}/*else{
				$list[$value['province']][$value['city']][$value['area']]['id'] = $value['area_id'];
				$list[$value['province']][$value['city']][$value['area']]['name'] = "{$value['province']}/{$value['city']}/{$value['area']}/{$value['street']}";
			}*/
		}
		$provice = array_unique($provice);
		$return['provice'] = $provice;
		$return['list'] = $list;
		return $return;
	}
}
